from concurrent.futures.thread import ThreadPoolExecutor
from flask import Blueprint
conversation_dify_bp = Blueprint('conversation_dify', __name__)
from Test import db
from user.SystemUser import *
from datetime import datetime, timedelta
import uuid
from dify.difyApp import *
from config.config import *
import pymysql
import base64


class ChatInfoDify(db.Model):
    __tablename__ = 'chat_info_dify'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=True)
    chat_id = db.Column(db.String(255), nullable=True)
    message_id = db.Column(db.String(255), nullable=True)
    message_type = db.Column(db.String(255), nullable=True)
    message_content = db.Column(db.Text, nullable=True)
    message_time = db.Column(db.DateTime, nullable=True)
    message_owner = db.Column(db.String(255), nullable=True)
    status = db.Column(db.Integer, nullable=True)
    related = db.Column(db.String(255), nullable=True)


def to_dict_value(self):
    return {
        'id': self.id,
        'user_id': self.user_id,
        'chat_id': self.chat_id,
        'message_id': self.message_id,
        'message_type': self.message_type,
        'message_content': self.message_content,
        'message_time': self.message_time,
        'message_owner': self.message_owner,
        'status': self.status,
        'related': self.related,
    }

## 由于dify请求时间较长，使用异步请求
threadPoolExecutor = ThreadPoolExecutor(20)

# 请求dify生成报告并写入数据库
def asynRequestDify(user_id, chat_id, question, dify_chat_data, user_preference, is_vip):
    try:
        logging.error('asyn start')
        apikey = 'app-vOJRAS6Guc0eP4R2BUpNKDLN'
        inputs_data = {
            'lang_type': dify_chat_data['lang_type'],
            'main_response': dify_chat_data['main_response'],
            'user_preference': json.dumps(user_preference)
        }
        #dify_chat = dify_chat_api(question, inputs_data, apikey)
        testdata = {
          "report": "PHNjcmlwdCBzcmM9Imh0dHBzOi8vY2RuLmJvb3RjZG4ubmV0L2FqYXgvbGlicy9lY2hhcnRzLzUuMy4zL2VjaGFydHMubWluLmpzIj48L3NjcmlwdD48c3R5bGU+Ym9keXtmb250LWZhbWlseTpBcmlhbCxzYW5zLXNlcmlmO2xpbmUtaGVpZ2h0OjEuNjtjb2xvcjojMzMzO21heC13aWR0aDoxMDAlO21hcmdpbjowIGF1dG87c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MjAyMuW5tO+8jOS4reWbveeahEdEUOi+vuWIsDEyMS4wMuS4h+S6v+WFg+S6uuawkeW4ge+8jOiAjOe+juWbveeahEdEUOS4ujI1LjTkuIfkur/nvo7lhYPjgILln7rkuo7mgqjnmoTmipXotYTlgY/lpb3vvIzkuobop6Pov5nkuKTlm73nu4/mtY7mgLvph4/nmoTlj5jljJblr7nmgqjnmoTmipXotYTlhrPnrZboh7PlhbPph43opoHjgIIyMDIz5bm077yM576O5Zu9R0RQ6aKE6K6h5ZCM5q+U5aKe6ZW/Mi41Je+8jOaYvuekuuWHuueos+WBpeeahOe7j+a1juWkjeiLj+WKv+WktOOAguWwveeuoeS4reWbvTIwMjPlubTnmoTlhbfkvZPmlbDmja7lvoXnoa7orqTvvIzkvYbmjIHnu63nmoTnu4/mtY7lop7plb/otovlir/kuLrmgqjnmoTmipXotYTmj5DkvpvkuoblnZrlrp7nmoTln7rnoYDjgILmgqjlj6/ku6XogIPomZHlnKjov5nkuKTlpKfnu4/mtY7kvZPkuK3lr7vmib7lhbflpIflop7plb/mvZzlipvnmoTooYzkuJrvvIzlpoLnp5HmioDlkozlj6/lho3nlJ/og73mupDvvIzku6XkvJjljJbmgqjnmoTmipXotYTnu4TlkIjjgII8L3A+PGgyPjIwMjEtMjAyMuW5tOS4ree+juWkseS4mueOh+WvueavlDwvaDI+PHA+MjAyMeW5tO+8jOe+juWbveeahOWkseS4mueOh+S4ujYuMiXvvIzogIzkuK3lm73nmoTlpLHkuJrnjofkuLrlvoXnoa7orqTjgILliLDkuoYyMDIy5bm077yM5Lit5Zu955qE5aSx5Lia546H6ZmN6IezNS42Je+8jOiAjOe+juWbveWImei/m+S4gOatpeS4i+mZjeiHszMuNSXjgILov5nkuIDlj5jljJbooajmmI7nvo7lm73lirPliqjlipvluILlnLrnmoTlvLrlirLmgaLlpI3vvIzkuLrmipXotYTogIXmj5Dkvpvkuobmm7TlpJrnmoTluILlnLrkv6Hlv4PjgILpq5jlsLHkuJrnjofpgJrluLjmhI/lkbPnnYDmtojotLnogIXmlK/lh7rlop7liqDvvIzmvZzlnKjlnLDmjqjliqjnm7jlhbPooYzkuJrnmoTlj5HlsZXjgILln7rkuo7ov5nkuIDotovlir/vvIzmgqjlj6/ku6XlhbPms6jkuI7mtojotLnogIXmlK/lh7rnm7jlhbPnmoTooYzkuJrvvIzlpoLpm7bllK7lkozmnI3liqHkuJrvvIzlnKjljJfnvo7luILlnLrkuK3lr7vmib7mipXotYTmnLrkvJrjgII8L3A+PGgyPuaKlei1hOW7uuiuruS4juetlueVpTwvaDI+PHA+5Z+65LqO5LiK6L+w5pWw5o2u5YiG5p6Q77yM5Lul5LiL5piv6ZKI5a+55oKo55qE5oqV6LWE5bu66K6u77yaCjEuICoq5aSa5YWD5YyW5oqV6LWE57uE5ZCIKirvvJrogIPomZHlsIbmipXotYTliIbmlaPliLDkuK3lm73lkoznvo7lm73nmoTlhbPplK7ooYzkuJrvvIzlpoLnp5HmioDjgIHljLvnlpflkozlj6/lho3nlJ/og73mupDvvIzku6XlubPooaHpo47pmankuI7mlLbnm4rjgIIKMi4gKirlhbPms6jnu4/mtY7lop7plb/mjIfmoIcqKu+8muaMgee7rei3n+i4quS4ree+juS4pOWbveeahEdEUOWinumVv+WSjOWkseS4mueOh+WPmOWMlu+8jOS7peWKqOaAgeiwg+aVtOaKlei1hOetlueVpe+8jOaKk+S9j+W4guWcuuacuumBh+OAggozLiAqKuWIqeeUqOWMuuWfn+S8mOWKvyoq77ya5Zyo5qyn5rSy5ZKM5YyX576O5biC5Zy65Lit5a+75om+5pS/562W5pSv5oyB5Yqb5bqm5aSn55qE6KGM5Lia77yM5bCk5YW25piv5Zyo6LSi5pS/5pS/562W6LCD5pW05LiL5Y+v6IO95Y+X55uK55qE6aKG5Z+f44CCCjQuICoq6ZW/5pyf6KeG6KeSKirvvJrnu5PlkIjlvbzlvpfCt+W+t+mygeWFi+eahOmVv+acn+inhuinku+8jOmAieaLqeWFt+Wkh+WPr+aMgee7reWinumVv+a9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QpO3ZhciBvcHRpb249SlNPTi5wYXJzZShjaGFydEVsZW1lbnQuZ2V0QXR0cmlidXRlKCdkYXRhLW9wdGlvbicpKTtjaGFydC5zZXRPcHRpb24ob3B0aW9uKX0pO2Z1bmN0aW9uIHJlc2l6ZUNoYXJ0cygpe2NoYXJ0cy5mb3JFYWNoKGZ1bmN0aW9uKGNoYXJ0RWxlbWVudCl7dmFyIGNoYXJ0PWVjaGFydHMuZ2V0SW5zdGFuY2VCeURvbShjaGFydEVsZW1lbnQpO2lmKGNoYXJ0KXtjaGFydC5yZXNpemUoKX19KX13aW5kb3cuYWRkRXZlbnRMaXN0ZW5lcigncmVzaXplJyxyZXNpemVDaGFydHMpfWNhdGNoKGVycm9yKXtjb25zb2xlLmVycm9yKCdFQ2hhcnRz5Yid5aeL5YyW6ZSZ6K+vOicsZXJyb3IpfX0pOzwvc2NyaXB0Pg=="
        }
        dify_chat = {
            'status': 200,
            'data': testdata
        }
        time.sleep(10)
        now = datetime.now()
        if dify_chat['status'] == 200:
            dify_chat_data = dify_chat['data']
            report_html = base64.b64decode(dify_chat_data['report'])
            conn = pymysql.connect(host=Config.host, user=Config.username, password=Config.password, db=Config.database_name)
            try:
                # 插入报告数据
                with conn.cursor() as cursor:
                    sql = "INSERT INTO `chat_report` (`user_id`, `question`, `content`, `create_time`, `update_time`) VALUES (%s, %s, %s, %s, %s);"
                    val = (user_id, question, report_html, now, now)
                    cursor.execute(sql, val)
                conn.commit()

                # 插入报告生成通知消息并关联报告id
                last_id = cursor.lastrowid
                message_id = uuid.uuid4().__str__()
                with conn.cursor() as cursor:
                    sql = "INSERT INTO `chat_info_dify` (`user_id`, `chat_id`, `message_id`, `message_type`, `message_content`, `message_time`, `message_owner`, `related`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s);"
                    val = (user_id, chat_id, message_id, 'report', 'Report generated', now, 'system', last_id)
                    cursor.execute(sql, val)
                conn.commit()

                # 如果非会员，扣除试用次数
                if is_vip == 0:
                    with conn.cursor() as cursor:
                        sql = "UPDATE sys_user SET free_times = free_times - 1 WHERE user_id=(%s)"
                        val = (user_id)
                        cursor.execute(sql, val)
                    conn.commit()
            finally:
                conn.close()

    except Exception as e:
        logging.error('Exception：%s', str(e))
    return

## 用户提问
@conversation_dify_bp.route('/getNewData', methods=['POST'])
@token_required
def getDifyData():
    try:
        request_data = request.json
        ##用户问题
        question = request_data.get('question')
        now = datetime.now()
        is_vip = 0

        user = User.query.get(g.user_id)

        if user.vip_expire and user.vip_expire > now :
            is_vip = 1
        # 需要有免费次数或者开通了会员才能使用
        if user.free_times < 1 and is_vip == 0 :
            return jsonify({
                'data':{},
                'status': 400,
                'errorMessage': 'need vip'
            })

        # 防止频繁提问
        seconds_ago = 2  # 几秒前
        time_seconds_ago = now - timedelta(seconds=seconds_ago)
        last_message = ChatInfoDify.query.filter_by(user_id=g.user_id).filter_by(message_owner='user').order_by(ChatInfoDify.message_time.desc()).first()
        if last_message and last_message.message_time > time_seconds_ago:
            return jsonify({
                'data':{},
                'status': 400,
                'errorMessage': 'wait'
            })

        seconds_ago = 90  # 几秒前
        time_seconds_ago = now - timedelta(seconds=seconds_ago)
        last_message = ChatInfoDify.query.filter_by(user_id=g.user_id).filter_by(message_owner='user').filter(ChatInfoDify.status == 1).order_by(ChatInfoDify.message_time.desc()).first()
        if last_message and last_message.message_time > time_seconds_ago:
            return jsonify({
                'data':{},
                'status': 400,
                'errorMessage': 'generating'
            })

        # 保存提问
        message_id = uuid.uuid4().__str__()
        new_chat = ChatInfoDify(
            user_id=user.user_id,
            chat_id=user.chat_id,
            message_id=message_id,
            message_type='question',
            message_content=question,
            message_time=datetime.now(),
            message_owner='user',
            status=0
        )
        db.session.add(new_chat)
        db.session.commit()

        # 偏好
        preference_list = []
        if user.preference:
            preference_list = json.loads(user.preference)
        industry_list = []
        if user.industry:
            industry_list = json.loads(user.industry)
        target_list = []
        if user.target:
            target_list = json.loads(user.target)
        preference_area_list = []
        if user.preference_area:
            preference_area_list = json.loads(user.preference_area)
        user_preference = {
            'occupation': user.occupation,
            'preference': preference_list,
            'industry': industry_list,
            'target': target_list,
            'preference_area': preference_area_list,
        }

        # 同步请求dify当前提问是否能生成报告
        apikey = 'app-iYkXe02XydMMqqfbeGxTzKTO'
        #dify_chat = dify_chat_api(question, {'user_preference' : json.dumps(user_preference)}, apikey)
        dify_chat = {
            'status': 200,
            'data': {
                "type": "report",
                "main_response": "test，调试使用",
                "lang_type": "{LangType}: 【中文】",
                "extend": "报告正在生成中"
            }
        }

        if dify_chat['status'] == 200:
            dify_chat_data = dify_chat['data']
            # 根据消息类型进行处理
            # 如果能直接生成报告
            if dify_chat_data['type'] == 'report':
                # 保存提问
                message_id = uuid.uuid4().__str__()
                new_chat = ChatInfoDify(
                    user_id=user.user_id,
                    chat_id=user.chat_id,
                    message_id=message_id,
                    message_type='text',
                    message_content=dify_chat_data['extend'],
                    message_time=datetime.now(),
                    message_owner='system',
                    status=0
                )
                db.session.add(new_chat)
                db.session.commit()
                # 异步调用dify生成报告
                threadPoolExecutor.submit(asynRequestDify, user.user_id, user.chat_id, question, dify_chat_data, user_preference, is_vip)
                # asynRequestDify(question, dify_chat_data, user_preference, user)

            # 如果需要重新问
            if dify_chat_data['type'] == 'suggestion':
                # 保存提问
                message_id = uuid.uuid4().__str__()
                new_chat = ChatInfoDify(
                    user_id=user.user_id,
                    chat_id=user.chat_id,
                    message_id=message_id,
                    message_type='suggestion',
                    message_content=dify_chat_data['extend'],
                    message_time=datetime.now(),
                    message_owner='system',
                    status=0
                )
                db.session.add(new_chat)
                db.session.commit()
            return jsonify({
                'message': 'success',
                'status': 200
            })
        else:
            return jsonify({
                'data': [],
                'message': 'exception',
                'status': 400
            })
    except Exception as e:
        logging.error('异常,错误原因为：%s', str(e))
        return jsonify({
            'data': '',
            'message': 'exception',
            'status': 500
        })

##获取历史消息，用户刷新页面初始化最近消息、滚动进度条查看更多历史消息
@conversation_dify_bp.route('/chat/history', methods=['GET'])
@token_required
def get_chat_history():
    try:
        min_id = request.args.get('min_id', 0, type=int)

        query = ChatInfoDify.query.filter_by(user_id=g.user_id)
        if min_id > 0 :
            query = query.filter(ChatInfoDify.id.__lt__(min_id))
        query = query.order_by(ChatInfoDify.id.desc()).limit(10)
        list = query.all()

        data = [to_dict_value(msg) for msg in list]

        # 如果是第一次进入（还没有消息），发送一条提示消息
        if min_id == 0 and len(data) == 0 :
            user = User.query.get(g.user_id)
            message_id = uuid.uuid4().__str__()
            new_chat = ChatInfoDify(
                user_id = user.user_id,
                chat_id = user.chat_id,
                message_id = message_id,
                message_type = 'remind',
                message_content = 'New users can ask five free questions, we suggest you consider an <a href="/user_center.html">upgrade</a> to basic or Pro membership to continue',
                message_time = datetime.now(),
                message_owner = 'system',
                status=0
            )
            db.session.add(new_chat)
            db.session.commit()

        return jsonify({
            'data': {
                'data': data,
            },
            'message': 'success',
            'status': 200
        })
    except Exception as e:
        logging.error('获取用户历史消息失败，失败原因：%s', str(e))
        return jsonify({
            'data': [],
            'message': 'exception',
            'status': 500
        })

##获取最新消息，用于前端定时任务请求最新消息等
@conversation_dify_bp.route('/chat/new', methods=['GET'])
@token_required
def get_chat_new():
    try:
        max_id = request.args.get('max_id', 0, type=int)

        query = ChatInfoDify.query.filter_by(user_id=g.user_id)
        if max_id > 0 :
            query = query.filter(ChatInfoDify.id.__gt__(max_id))
        query = query.order_by(ChatInfoDify.id.desc()).limit(10)
        list = query.all()

        data = [to_dict_value(msg) for msg in list]

        return jsonify({
            'data': {
                'data': data,
            },
            'message': 'success',
            'status': 200
        })
    except Exception as e:
        logging.error('获取用户历史消息失败，失败原因：%s', str(e))
        return jsonify({
            'data': [],
            'message': 'exception',
            'status': 500
        })


@conversation_dify_bp.route('/get-chat-messages-deail', methods=['POST'])
@token_required
def get_chat_dify_message_detail():
    try:
        request_data = request.json
        message_id = request_data.get('message_id')

        ##用户信息
        token = request.headers.get('Authorization')
        decoded = decode_token(token)
        current_user = decoded['username']

        query = ChatInfoDify.query.filter_by(message_owner=current_user)
        query_data = query.filter(ChatInfoDify.message_id == message_id).first()

        return jsonify({
            'data': to_dict_value(query_data),
            'message': 'get message detail by message_id success',
            'status': 200
        })
    except Exception as e:
        logging.error('获取消息详情失败，失败原因：%s', str(e))
        return jsonify({
            'data': {},
            'message': 'get message detail by message_id failed',
            'status': 500
        })
